package com.flkin.security;

import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 401 未认证
 *
 * @author yanxiang.huang
 */
@Component
public class IAuthenticationEntryPoint implements AuthenticationEntryPoint {

    private final static String UNAUTHORIZED = "Unauthorized";

    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException {
        httpServletResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED, e == null ? UNAUTHORIZED : e.getMessage());
    }

}
